<template>
	<basic-container>
		<avue-crud :option="option" :data="data" :page="page" v-model="form" ref="crud" @search-change="searchChange"
		 @search-reset="searchReset" @current-change="currentChange" @size-change="sizeChange" @row-update="rowUpdate"
		 @row-save="rowSave" @row-del="rowDel" @on-load="onLoad" :before-open="beforeOpen">
			<template slot-scope="scope" slot="menuForm">
				<el-button type="primary" icon="el-icon-check" :loading="loading" size="mini" @click="submit">提交</el-button>
			</template>
			<template slot="menuRight">
				<el-radio-group @change="searchReset" v-model="status" size="mini" style="margin-right: 50px;">
					<el-radio-button label="">所有</el-radio-button>
					<el-radio-button :label="item.value" v-for="(item,i) in statusItem" :key="i">{{item.label}}</el-radio-button>
				</el-radio-group>
			</template>
			<template slot="expand" slot-scope="{row}">
				<el-row :gutter="6">
					<el-col :span="1"></el-col>
					<el-col :span="3">
						条码
					</el-col>
					<el-col :span="3">
						库位
					</el-col>
					<el-col :span="2">
						库存数量
					</el-col>
					<el-col :span="15">
						盘点数量
					</el-col>
				</el-row>
 				<el-row :gutter="6" v-for="(item,index) in row.details" :key="index">
					<el-col :span="1"></el-col>
					<el-col :span="3">
						<el-tag size="mini">{{item.barCode}}</el-tag>
					</el-col>
					<el-col :span="3">
						{{item.localCode}}
					</el-col>
					<el-col :span="2">
						{{item.quantity}}
					</el-col>
					<el-col :span="15">
						{{item.checkQty}}
					</el-col>
				</el-row>
			</template>
			<template slot-scope="scope" slot="menu">
				<el-button type="text" size="mini" icon="el-icon-edit" v-if="scope.row.status==0" @click.stop="$refs.crud.rowEdit(scope.row,scope.index)">编辑</el-button>
				<el-button type="text" size="mini" icon="el-icon-delete" v-if="scope.row.status==0" @click.stop="$refs.crud.rowDel(scope.row)">删除</el-button>
			</template>
			<template slot-scope="{row}" slot="status">
				<el-tag v-for="(item,i) in statusItem" size="mini" :key="i" :type="item.type" v-if="item.value == row.status">{{item.label}}</el-tag>
			</template>
			<template slot-scope="{row}" slot="detailsForm">
				<avue-crud id="subTable" :option="suboption" :data="subform" :before-open="subOpen">
					<template slot-scope="{row}" slot="remark">
						<el-input style="width: 100%;" v-model="row.remark"></el-input>
					</template>
				</avue-crud>
			</template>
		</avue-crud>
		<el-dialog title="盘点详情" width="1000px" :visible.sync="dialogShow" v-if='dialogShow' append-to-body
		 :modal-append-to-body='false'>
			<check :storeCode="form.storeCode" :selectionList="sublist" @closeMoule="closeMoule">
			</check>
		</el-dialog>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';
	import check from './select.vue'

	export default {
		components: {
			check
		},
		data() {
			return {
				type: '',
				loading: false,
				status: "",
				statusItem: [{
					value: 0,
					type: 'info',
					label: '暂存'
				}, {
					value: 1,
					type: '',
					label: '已提交'
				}, {
					value: 2,
					type: 'warning',
					label: '已盘点'
				}, {
					value: 7,
					type: 'success',
					label: '已完成'
				}],
				drawer: false,
				direction: 'rtl',
				dialogShow: false,
				form: {
					details: []
				},
				subform: [],
				sublist: {},
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					expand: true,
					dialogDrag: true,
					searchMenuSpan: 4,
					border: true,
					index: true,
					viewBtn: false,
					refreshBtn: false,
					excelBtn: true,
					editBtn: false,
					delBtn: false,
					menuWidth: '120px',
					column: [
						{
							label: "盘点单号",
							prop: "code",
							display: false,
							searchSpan:4,
							search: true,
							width: 150,
							placeholder: "非必填，可自动生成"
						},
						{
							label: '类型',
							prop: 'type',
							search: true,
							searchSpan:4,
							searchFilterable: true,
							filterable: true,
							clearable: false,
							width: 80,
							type: 'select',
							dicData: [],
							props: {
								label: 'name',
								value: 'sort'
							},
							rules: [{
								required: true,
								trigger: 'blur'
							}]
						},
						{
							label: "所属仓库",
							prop: "storeCode",
							searchSpan: 6,
							search: true,
							filterable: true,
							searchFilterable: true,
							clearable: false,
							group: true,
							width: "250px",
							type: "select",
							dicData: [],
							rules: [{
								required: true,
								trigger: "blur"
							}],
							change: ({
								value,
								column
							}) => {
								if (value != '') {
									this.subform = []
								}
							}
						},
						{
							label: "库存数量",
							prop: "quantity",
							align:'right',
							display:false,
							width: "100px"
						},
						{
							label: "盘点数量",
							prop: "checkQty",
							align:'right',
							display:false,
							width: "100px"
						},
						{
							label: "备注",
							prop: "remark"
						},
						{
							label: '状态',
							prop: 'status',
							width: '80px',
							type: "radio",
							display: false,
							dicData: [],
							slot: true
						},
						{
							label: "盘点时间",
							prop: "createdOn",
							width: '120px',
							type: "date",
							format:'yyyy-MM-dd HH:mm',
							display: false
						},
						{
							label: "盘点人",
							prop: "createdBy",
							width: "100px",
							display: false
						},{
							label: '盘点详情',
							prop: 'details',
							formslot: true,
							hide: true,
							span: 24
						}
					]
				},
				data: [],
				suboption: {
					size: "mini",
					border: true,
					refreshBtn: false,
					editBtn: false,
					simplePage: true,
					menu: false,
					column: [{
							label: "所选库位",
							prop: "localCode",
							width: 150
						}, {
							label: "条码",
							prop: "barCode",
							width: 150
						},{
							label: "数量",
							prop: "quantity",
							width: 100
						},
						{
							label: "备注",
							prop: "remark",
							slot: true
						}
					]
				}
			};
		},
		created() {
			request({
				url: '/api/code/getCodes',
				params: {
					typeId: 'CODE201002'
				}
			}).then(res => {
				this.findObject(this.option.column, 'type').dicData = res.data.data;
			});
			request({
				url: '2/api/WmsStorage/groups',
			}).then(res => {
				this.findObject(this.option.column, 'storeCode').dicData = res.data.data;
			});
		},
		methods: {
			closeMoule(e) {
				this.subform = e;
				this.dialogShow = false;
			},
			beforeOpen(done, type) {
				this.type = type;
				if (["edit"].includes(type)) {
					request({
						url: '2/api/WmsCheck/getdetails',
						method: 'get',
						params: {
							type: this.form.code,
							limit: 0
						}
					}).then(res => {
						this.subform = res.data.data;
					});
				} else if (["add"].includes(type)) {
					this.subform = [];
				} else if (["view"].includes(type)) {
					this.drawer = true;
					return;
				}
				done();
			},
			subOpen() {
				if (this.form.storeCode == undefined || this.form.storeCode == '') {
					this.$message.warning("请选择所属仓库");
					return;
				}
				this.sublist.storeCode = this.form.storeCode;
				this.sublist.data = this.subform;
				this.dialogShow = true;
			},
			submit() {
				this.form.status = 1;
				if (this.type == "add") {
					this.$refs.crud.rowSave();
				} else if (this.type == "edit") {
					this.$refs.crud.rowUpdate();
				}
			},
			rowSave(row, loading, done) {
				if (row.status == null || row.status== ''){
					row.status = 0;
				}
				this.loading = true
				row.details = this.subform;
				request({
					url: '2/api/WmsCheck/add',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.loading = false;
					this.onLoad(this.page);
					done();
				}, () => {});
			},
			rowUpdate(row, index, loading, done) {
				this.loading = true
				row.details = this.subform;
				return request({
					url: '2/api/WmsCheck/edit',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.loading = false;
					this.onLoad(this.page);
					done();
				}, () => {});
			},
			rowDel(row) {
				this.$confirm("确定数据删除?")
					.then(() => {
						return request({
							url: '2/api/WmsCheck/remove',
							method: 'post',
							params: {
								id: row.id
							}
						});
					})
					.then(() => {
						this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				request({
					url: '2/api/WmsCheck/getpages',
					params: {
						key: this.query.code == undefined ? '' : this.query.code,
						type: this.query.type == undefined ? '' : this.query.type,
						status: this.status == undefined ? '' : this.status,
						where: this.query.storeCode == undefined ? '' : this.query.storeCode,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.page.total = data.remark;
				});
			}
		}
	};
</script>

<style>
	#subTable .avue-crud__pagination {
		position: relative;
		padding: 0px 0 0px 20px;
		text-align: right;
	}
</style>
